import Button = laya.ui.Button;

class MainView extends Panel{
    loadList = ["main"];
    ui : layaUI.main.MainViewUI;
    uiClass = layaUI.main.MainViewUI;
    tParent = UIManager.uiLayer;

    private static _instance:MainView;//单例

    private _model:Web3Manager;
    private _tabButtonList:Array<any> = [];//tab 按钮列表
    private _tabSelectButton:Laya.Button;//tab 当前选中的按钮
    private _tabSelectIndex:number = 0;//tab 当前选中index
    private _nowView:Laya.Sprite;//当前显示的页签

    private _firstShow:boolean = true;//是否是第一次显示

    constructor(){
        super();
        //检测语言
        if(Global.LANGUAGE == LanguageType.CHINESE){
            this.loadList.push("cn");
        }else{
            this.loadList.push("en");
        }
    }

    /**
     * 获取 单例
     */
    public static getInstance():MainView{
        if(MainView._instance == null){
            MainView._instance = new MainView();
        }
        return MainView._instance;
    }

    /**
     * 初始化
     */
    protected onFirst():void{
        //检测是否初始化
        this._tabButtonList = [this.ui.btn_1,this.ui.btn_2,this.ui.btn_3,this.ui.btn_4];
        //数据
        this._model = Web3Manager.getInstance();
        //移除所有子页
        this.ui.homePage1.removeSelf();
        this.ui.homePage2.removeSelf();
        this.ui.homePage3.removeSelf();
        this.ui.homePage4.removeSelf();
        //类型
        this.ui.homePage3.isShowMaster = false;
        this.ui.homePage4.isShowMaster = true;
        //添加侦听
        var button:Laya.Button;
        for(var i:number=0;i<this._tabButtonList.length;i++){
            button = this._tabButtonList[i];
            button.on(Laya.Event.CLICK,this,this.itemClickHandler);
        }
        Laya.stage.on(Laya.Event.RESIZE,this,this.onResize);
        this.ui.checkLang.selectedIndex = Global.LANGUAGE - 1;
        this.ui.checkLang.on(Laya.Event.CHANGE,this,this.langChangeHandler);
        //中文按钮
        LangUtils.setSkin(this.ui.btn_1);
        LangUtils.setSkin(this.ui.btn_2);
        LangUtils.setSkin(this.ui.btn_3);
        LangUtils.setSkin(this.ui.btn_4);
        //cdn加载
        ResUtils.setLoadSkin(this.ui.imgBig1);
        //显示聊天图标
        DiscordIcon.getInstance().open();
    }

    /**
     * 添加到舞台
     */
    protected onShow():void{
        this.onResize();
        if(this._firstShow == true){
            this._firstShow = false;
            this._tabSelectIndex = CookieUtils.getIndex();
            this._tabSelectIndex = isNaN(this._tabSelectIndex) ? 0 : this._tabSelectIndex;
        }
        this.setTabSelectIndex(this._tabSelectIndex);
        this._model.on(Web3Manager.ACCOUNT_CHANGE,this,this.onAccountChange);
        this.onAccountChange();
    }

    /**
     * 从舞台移除
     */
    protected onClose():void{
        this._model.off(Web3Manager.ACCOUNT_CHANGE,this,this.onAccountChange);
    }

    //语言变更
    private langChangeHandler():void{
        CookieUtils.saveLanguage(this.ui.checkLang.selectedIndex + 1);
    }

    //舞台大小变更
    private onResize():void{
        this.ui.mainPanel.height = StageManager.height;
    }

    //tab item点击
    private itemClickHandler(e:Laya.Event):void{
        var index:number = this._tabButtonList.indexOf(e.target);
        this.setTabSelectIndex(index);
    }

    //设置tab选中index
    public setTabSelectIndex(index:number):void{
        if(this.ui == null || this.ui.parent == null || index == -1){
            this._tabSelectIndex = index;
            return;
        }
        if(index != this._tabSelectIndex || this._tabSelectButton == null){
            //自己的历史记录 检测登录信息
            if(index == 3 && Web3Manager.checkDefAccountIsEmpty() == true){
                Alert.getInstance().showMsg = LangUtils.getText(18);
                Alert.getInstance().open();
                return;
            }
            if(this._tabSelectButton != null){
                this._tabSelectButton.selected = false;
            }
            var clickButton:Laya.Button = this._tabButtonList[index];
            this._tabSelectButton = clickButton;
            this._tabSelectButton.selected = true;
            this._tabSelectIndex = index;
            this.onTab(index);
        }
    }

    private onTab(index):void{
        CookieUtils.saveIndex(index == 3 ? 0 : index);
        this.ui.mainPanel.vScrollBar.value = 0;
        if(this._nowView != null){
            this._nowView.visible = false;
            this._nowView['removeEvent']();
            this._nowView.parent.removeChild(this._nowView);
        }
        this._nowView = this.ui["homePage" + (index+1)];
        if(this._nowView != null){
            this._nowView.visible = true;
            this._nowView['addEvent']();
            this.ui.mainPanel.addChild(this._nowView);
        }
    }

    private onAccountChange():void{
        if(Web3Manager.defaultAccount !="" && Web3Manager.defaultAccount != null){
            this.ui.txt1.text = Utils.getShortAddress(Web3Manager.defaultAccount);
        }else{
            this.ui.txt1.text = LangUtils.getText(27);
        }
    }
}